//
//  LXHomeViewController.m
//  灵秀
//
//  Created by 李田 on 17/3/18.
//  Copyright © 2017年 tianyao. All rights reserved.
//

#import "LXHomeViewController.h"
#import "LXThreeBtnView.h"

#define topHeight 123
@interface LXHomeViewController ()<UIScrollViewDelegate, LXThreeBtnViewDelegate, UIGestureRecognizerDelegate>
// topView
@property(nonatomic, weak)UIView *topView;
//scrollView
@property(nonatomic, weak)UIScrollView *scrollView;
// threeBtnView
@property(nonatomic, weak)LXThreeBtnView *threeBtnView;


@end

@implementation LXHomeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
}

#pragma mark - 5.0解决手势冲突
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
    
    return YES;
}

#pragma mark - 4.0点击按钮实现切换scrolView
- (void)threeBtnView:(LXThreeBtnView *)threeBtnView withIndex:(NSInteger)index {
    
    CGRect rect = CGRectMake(index * kScreenWidth, 0, kScreenWidth, _scrollView.bounds.size.height);
    // 滚动到指定位置
    [_scrollView scrollRectToVisible:rect animated:YES];
}

#pragma mark - 3.0 手动滚动scrolVIew实现小黄条滚动
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    
    // 获取偏移量
    CGPoint offset = scrollView.contentOffset;
    
    // 判断是手动还是自动
    //    NSLog(@"%zd   %zd  %zd",scrollView.tracking,scrollView.dragging,scrollView.decelerating);
    if (!(scrollView.tracking || scrollView.dragging || scrollView.decelerating)) {//自动
        return;
    }
    self.threeBtnView.offsetX = offset.x / 3;
}

#pragma mark - 1.0 界面搭建
- (void)setupUI {
    
    //设置导航条的透明度0
    self.navigationController.navigationBar.alpha = 0;
    
    //MARK:1.topView
    UIView *topV = [[UIView alloc]init];
    topV.backgroundColor = [UIColor purpleColor];
    [self.view addSubview:topV];

    //MARK:2.threeBtnView
    LXThreeBtnView *threeV = [[LXThreeBtnView alloc]init];
    // 设置代理
    threeV.delegate = self;
    [self.view addSubview:threeV];
    
    //MARK:3.scrolView
    UIScrollView *scrolV = [[UIScrollView alloc]init];
    scrolV.backgroundColor = [UIColor brownColor];
    //分页
    scrolV.pagingEnabled = YES;
    //弹动
    scrolV.bounces = NO;
    //滚动条
    scrolV.showsHorizontalScrollIndicator = NO;
    // 设置代理
    scrolV.delegate = self;
    [self.view addSubview:scrolV];
    
    //关联
    _topView = topV;
    _scrollView = scrolV;
    _threeBtnView = threeV;
    
    //布局
    [topV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.equalTo(self.view);
        make.height.mas_equalTo(topHeight);
    }];
    
    [threeV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(topV.mas_bottom);
        make.left.right.equalTo(self.view);
        make.height.mas_equalTo(37);
    }];
    
    [scrolV mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.right.bottom.equalTo(self.view);
        make.top.equalTo(threeV.mas_bottom);
    }];
    
    //MARK: 4.添加拖拽手势,监听手势 修改topV 高度约束
    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)];
    //解决手势冲突 设置的代理
    pan.delegate = self;
    [self.view addGestureRecognizer:pan];
    
    //    MARK: 5.添加点菜 评价 商家 三个控制器的view
    NSArray <NSString *>*vcNameArray = @[@"LXOrderViewController", @"LXCommentViewController", @"LXBusinessViewController"];
    NSMutableArray <UIView *>*viewArray = [NSMutableArray arrayWithCapacity:10];
    [vcNameArray enumerateObjectsUsingBlock:^(NSString * _Nonnull vcName, NSUInteger idx, BOOL * _Nonnull stop) {
        //1.根据字符串  class
        Class class = NSClassFromString(vcName);
        //2.根据类 生成对象
        UIViewController *targetVc = [[class alloc]init];
        //3.添加
        [self cz_addChildController:targetVc intoView:scrolV];
        //4.将VIew 添加集合
        [viewArray addObject:targetVc.view];
    }];
    
    // 将三个控制器的view设置约束
    [viewArray mas_distributeViewsAlongAxis:MASAxisTypeHorizontal withFixedSpacing:0 leadSpacing:0 tailSpacing:0];
    [viewArray mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.equalTo(scrolV);
        make.size.equalTo(scrolV);
    }];
    
}

#pragma mark - 2.0 监听拖拽的手势
- (void)pan: (UIPanGestureRecognizer *)sender {
    
    //MARK:1.获取偏移量
    CGPoint offset = [sender translationInView:sender.view];
    
    //MARK:2.归零
    [sender setTranslation:CGPointZero inView:sender.view];
    
    //MARK:3.计算高度
    CGFloat height = _topView.bounds.size.height  + offset.y;
    
    
    //MARK:5.判断
    if((height > topHeight)  || (height < kNavBarH)){
        
        return;
    }
    
    //MARK:4.更新topV的高度的约束
    [_topView mas_updateConstraints:^(MASConstraintMaker *make) {
        
        make.height.mas_equalTo(height);
        
    }];
    
    // 建议加个动画
    [UIView animateWithDuration:.3 animations:^{
        [self.view layoutIfNeeded];
    }];
    
    //MARK:6.计算透明度
    //透明度的比例
    CGFloat scale = 1.0/(topHeight - kNavBarH);
    
    CGFloat alpha =  1 - (height - kNavBarH) * scale;
    
    self.navigationController.navigationBar.alpha = alpha;
}

@end












